package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * ClassName: OrderMapper.xml
 * Description: com.sky.mapper
 */
@Mapper
public interface OrderMapper {

    /**
     * 向订单表中插入数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);


    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 用于替换微信支付更新数据库状态的问题
     * @param orderStatus
     * @param orderPaidStatus
     */
    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String orderNumber);

    /**
     * 催单
     * @param id
     * @param deliveryStatus
     */
    @Update("update orders set delivery_status = #{deliveryStatus} where id = #{id}")
    void updateDeliveryStatus(Long id, Integer deliveryStatus);

    /**
     * 各个状态的订单数量统计
     * @param status
     * @return
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    /**
     * 获取超时的订单
     * @param status
     * @param check_out_time
     * @return
     */
    @Select("select* from orders where status = #{status} and order_time < #{check_out_time}")
    List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime check_out_time);

    /**
     * 计算一天的营业额
     * @param beginTime
     * @param endTime
     * @param status
     * @return
     */
    @Select("select sum(amount) from orders where order_time between #{beginTime} and #{endTime} and status = #{status}")
    Double sumMoney(LocalDateTime beginTime, LocalDateTime endTime, int status);

    /**
     * 统计订单数量
     * @param map
     * @return
     */
    Integer countOrders(Map map);

    /**
     * 获取订单的id
     * @param begin
     * @param end
     * @return
     */
    @Select("select id from orders where order_time between #{begin} and #{end} and status = #{status}")
    List<Integer> getOrderId(LocalDateTime begin, LocalDateTime end, Integer status);
}
